package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.entity.sys.SysMenu;
import com.atguigu.spzx.model.vo.common.R;
import com.atguigu.spzx.model.vo.sys.SysMenuVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "菜单管理模块")
@RequestMapping("/admin/system/menu")
public class SysMenuController {
    @Resource
    SysMenuService sysMenuService;
    @Operation(summary = "查询登录用户可访问的菜单列表")
    @GetMapping("listUserMenu")
    public R listUserMenu(){
        //返回一级菜单集合
        List<SysMenuVO> sysMenuVOS =  sysMenuService.listUserMenu();
        return R.ok().data("items",sysMenuVOS);// r.setData(map)  替换R对象之前的map
    }
    @Operation(summary = "查询所有菜单和用户已分配的菜单id集合")
    @GetMapping("listMenusAndAssignedMenuId/{roleId}")
    public R listMenusAndAssignedMenuId(@PathVariable("roleId")Long roleId){
        Map<String,Object> map = sysMenuService.listMenusAndAssignedMenuId(roleId);
        return R.ok().data(map);// r.setData(map)  替换R对象之前的map
    }
    @Operation(summary = "更新菜单")
    @PutMapping("update")
    public R update(@RequestBody SysMenu sysMenu){
        sysMenuService.update(sysMenu);
        return R.ok();
    }

    @Operation(summary = "查询指定菜单")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id")Long id){
        //查询一级菜单集合
        //二级菜单集合设置到了一级菜单的children集合中
        SysMenu sysMenu = sysMenuService.getById(id);
        return R.ok("item" , sysMenu);
    }

    @Operation(summary = "新增菜单:必须携带pid关联菜单")
    @PostMapping("save")
    public R save(@RequestBody SysMenu sysMenu){
        //查询一级菜单集合
        //二级菜单集合设置到了一级菜单的children集合中
        sysMenuService.save(sysMenu);
        return R.ok();
    }

    @Operation(summary = "查询菜单嵌套集合")
    @GetMapping("listNestedMenus")
    public R listNestedMenus(){
        //查询一级菜单集合
        //二级菜单集合设置到了一级菜单的children集合中
        List<SysMenu> sysMenuList = sysMenuService.listNestedMenus();
        return R.ok("items" , sysMenuList);
    }
    @Operation(summary = "删除指定菜单")
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id")Long id){
        sysMenuService.delete(id);
        return R.ok();
    }
}
